﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;



namespace backoffice.Data
{
    public class Fielddb
    {
        private static db2growDataContext db = new db2growDataContext();
        
        public static void parseCSV()
        {
            
    /*        Guid ex = (from c in db.tbCountries
                       where c.Name.Equals("Mozambique")
                       select c.CountryID).First();



            String path = "C:\\Users\\Tony Dias\\Desktop\\mozambique_zone_plants.csv";
            
            List<String[]> parseData = new List<String[]>();


            try
            {
                using (StreamReader readFile = new StreamReader(path))
                {
                    String line;
                    String[] column;
                    
                    while ((line = readFile.ReadLine()) != null)
                    {
                        column = line.Split(';');
                        parseData.Add(column);
                    }
                }
            }
            catch (Exception e)
            {
                Console.WriteLine(e.Message);
            }


            foreach (String[] l in parseData)
            {
                Guid reg = (from c in db.tbZones
                           where c.Name.Equals(l[0])
                           select c.ZoneID).First();

                Guid plant = (from p in db.tbPlants
                              where p.Name.Equals(l[1])
                              select p.PlantID).First();

                tbZones_Plant tb = new tbZones_Plant();
                tb.ZonePlantID = Guid.NewGuid();
                tb.ZoneID = reg;
                tb.PlantID = plant;

                if(l[2].Equals("")){
                    tb.PlantingOnset = null;
                }else{tb.PlantingOnset = l[2];}
                 if(l[3].Equals("")){
                    tb.PlantingEnd = null;
                 }else{tb.PlantingEnd = l[3];}
                 if (l[4].Equals(""))
                 {
                     tb.PlantingRate = null;
                 }
                 else { tb.PlantingRate = l[4]; }
                 if (l[5].Equals(""))
                 {
                     tb.PlantingRateUnit = null;
                 }
                 else
                 {
                     tb.PlantingRateUnit = l[5];
                 }
                 if (l[6].Equals(""))
                 {
                     tb.CycleLength = null;
                 }
                 else
                 {
                     tb.CycleLength = l[6];
                 }
                 if ((l[7].Equals("")))
                 {
                     tb.HarvestingOnset = null;
                 }
                 else
                 {
                     tb.HarvestingOnset = l[7];
                 }
                 if ((l[8].Equals("")))
                 {
                     tb.HarvestingEnd = null;
                 }
                 else
                 {
                     tb.HarvestingEnd = l[8];
                 }

















                db.tbZones_Plants.InsertOnSubmit(tb);
            
            }

            */
           //foreach (String[] l in parseData)
           // {
                //Console.WriteLine(l[0]);
                //Console.WriteLine(l[1]);
                //Console.WriteLine(l[2]+"\n");

           /* ----- Plants --------
                
                tbPlant tb = new tbPlant();
                tb.PlantID = Guid.NewGuid();
                tb.Name = l[0];
                tb.ScientificName = l[1];
                tb.BotanicalFamily = l[2];
                if (l[3].Equals(""))
                {
                    tb.OtherNames = null;
                }
                else
                {
                    tb.OtherNames = l[3];
                }
                tb.Type = l[4];
                db.tbPlants.InsertOnSubmit(tb); */
           // }

            //db.SubmitChanges();
           
            
           // return parseData;
        }


            
           /* Guid ex = (from c in db.tbCountries
                     where c.Name.Equals("Mozambique")
                     select c.CountryID).First();

            int i;
            for (i = 1; i < 10; i++)
            {
                tbZone tbz = new tbZone();
                tbz.CountryID = ex;
                tbz.Name = "Region 0"+i;
                tbz.ZoneID = Guid.NewGuid();
                db.tbZones.InsertOnSubmit(tbz);
            }

            tbZone tbzz = new tbZone();
            tbzz.CountryID = ex;
            tbzz.Name = "Region 10";
            tbzz.ZoneID = Guid.NewGuid();
            db.tbZones.InsertOnSubmit(tbzz);

           
            db.SubmitChanges(); */

        /*
        public static void printList(List<Object> list)
        {
            foreach (Object element in list)
                Console.WriteLine(element);
        }

        public static void printArray(Object[] array)
        {
            foreach (Object element in array)
                Console.WriteLine(element);
        }

        */
    }
}
